package com.karrim.server.io.file;

import com.karrim.server.io.file.def.FileLocator;
import com.karrim.server.io.file.def.FileStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author andrea.cantafio
 */
public class FileStreamImpl implements FileStream {

    public FileStreamImpl(FileInputStream is) {
        this.is = is;
    }

    public FileStreamImpl(String path) throws FileNotFoundException {
        is = new FileInputStream(path);
    }

    @Override
    public FileInputStream open(FileLocator locable) {
        try {
            return tryOpen(locable);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileStreamImpl.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    @Override
    public FileInputStream tryOpen(FileLocator locable) throws FileNotFoundException {
        return (is = new FileInputStream(locable.get()));
    }

    @Override
    public FileInputStream get() {
        return is;
    }
    private FileInputStream is;
}
